<template>
  <div>
    <el-button icon="el-icon-plus" @click="openAddTemplateDialog()">新增模板</el-button>

    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="templateName"
        label="模板名称">
      </el-table-column>
      <el-table-column
        prop="templateType"
        label="模板类型">
      </el-table-column>
      <el-table-column
        prop="templateDesc"
        label="模板描述">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--模板信息弹窗-->
    <AddTemplate ref="templateDialog"></AddTemplate>
  </div>
</template>

<script>
  import AddTemplate from "./addTemplate";
  import templateApi from "@/api/gen/config/template";
  import {Notification} from 'element-ui';

  export default {
    name: "index",
    components: {AddTemplate},
    data() {
      return {
        tableData: []
      }
    },
    created() {
      this.getAllTemplate();
    },
    methods: {
      getAllTemplate(){
        templateApi.findAll().then(res=>{
          this.tableData = res.data;
        });
      },
      /*打开模板弹窗*/
      openAddTemplateDialog(data) {
        this.$refs.templateDialog.openDialog(data) // 方法2:直接调用
      },
      handleUpdate(data) {
        this.openAddTemplateDialog(data);
      },
      handleDelete(data, index) {
        templateApi.deleteOne(data.id).then(res=>{
          if (res.success) {
            Notification({
              title: '成功',
              message: '删除数据成功!',
              type: 'success',
              duration: 2000
            });
            this.getAllTemplate();
          } else {
            Notification({
              title: '失败',
              message: '删除数据失败!',
              type: 'error',
              duration: 2000
            })
          }
        })
      }

    }

  }
</script>

<style scoped>

</style>
